//
//  Bundle+APExtension.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/4/17.
//

import Foundation

public func jsonDict(forResource name: String) -> [String: Any]? {
    guard let jsonPath = Bundle.main.path(forResource: name, ofType: "json") else { return nil }
    var jsonData: Data?
    if #available(iOS 16.0, *) {
        guard let data = try? Data(contentsOf: URL(filePath: jsonPath)) else { return nil }
        jsonData = data
    } else {
        // Fallback on earlier versions
        guard let data = try? Data(contentsOf: URL(fileURLWithPath: jsonPath)) else { return nil }
        jsonData = data
    }
    guard let json = try? JSONSerialization.jsonObject(with: jsonData!) else { return nil }
    guard let dict = json as? [String: Any] else { return nil }
    return dict
}
